package com.perfree.controller;

import com.perfree.common.AjaxResult;
import com.perfree.common.TreeUtils;
import com.perfree.entity.Resource;
import com.perfree.service.ResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 资源管理树
 * </p>
 */
@Controller
@Slf4j
@RequestMapping("/resource")
@RequiredArgsConstructor(onConstructor_= {@Autowired})
public class ResourceTreeController  extends BaseController {

    private final ResourceService resourceService;

    @RequestMapping("/tree")
    public String index(Model model){
        model.addAttribute("showAddress",getUploadShowUrl());
        return "tree/resource";
    }

    @PostMapping("/insert")
    @ResponseBody
    public AjaxResult insert(@RequestBody Resource resource) {
        resourceService.insert(resource);
        return new AjaxResult(AjaxResult.AJAX_SUCCESS);
    }

    @GetMapping("/get/tree")
    @ResponseBody
    public AjaxResult getResourceTree() {
        List<Resource> list = resourceService.getResourceList();
        return new AjaxResult(AjaxResult.AJAX_SUCCESS, TreeUtils.getTreeList(list));
    }


    @GetMapping("/get/list")
    @ResponseBody
    public AjaxResult getResourceList() {
        List<Resource> list = resourceService.getResourceList();
        return new AjaxResult(AjaxResult.AJAX_SUCCESS, list);
    }
}
